﻿@model IEnumerable<Bordspil.Models.GroupMembers>

@{
    ViewBag.Title = "Index";
}

<ul id="group-menu">
    <li>@Html.ActionLink("Mínir hópar", "Index", "Group", new { userName = User.Identity.Name }, null)</li> 
    <li>@Html.ActionLink("Búa til hóp","CreateGroup", "Group")</li>
</ul>
<span class="leave-msg"></span>
<div id="groups">

    @if (Model.FirstOrDefault() == null)
    {
        <h4>Þú ert ekki í neinum hóp.</h4><br><br><br><br><br><br><br>
    }
    
@foreach (var item in Model)
{
    
    <div class="group-name">@Html.ActionLink(item.Group.GroupName, "GroupProfile", "Group", new { groupName = item.Group.GroupName },null) 
    @if (item.Group.UserOwnerId == item.UserId)
    {
        @Html.ActionLink("Breyta Hóp", "EditGroup", "Group", new { id = item.GroupId }, new {@class = "btn btn-info change-group-btn"})
        
      

    } else {
        <button name="@item.GroupId" class="leve-group btn btn-danger change-group-btn">Hætta í hóp</button>
    }
    <span id="@item.GroupId" class="see-members">Sjá meðlimi ▼</span>
        <ul class="member-list">

        </ul>
        <img src="/Content/ajax-loader.gif" alt="Hled upplysingum" class="loading"/>
    </div> 
    
}
</div>


<script src="~/Scripts/GroupScript.js"></script>
<script>
    $(document).ready(function () {

        $(".leve-group").click(function () {
            //ser um ad haetta i gruppu server ser um ad validatea gogn (hvort rettur notandi se ad segja sig ur gruppu ofl)
            var leveThisGroup = $(this).attr('name');
            
            var goner = { "groupId": leveThisGroup,
                "userName": '@User.Identity.Name' };


            $.post("/Group/LeveGroup", goner, function (data) {
                
                if (data === "Hættur") {
                    $(".leave-msg").html("Augnablik...");
                    window.location = "/Group?userName=@User.Identity.Name";
                } else {
                    $(".leave-msg").html(data);
                }
                console.log(data);
            });
        });

    });

</script>



